<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'
import { getAlertTotalByGroupId } from '@/api/iot/deviceLog';
const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '250px'
        },
        chartData: {
            type: Array,
            // required: true
        },
        groupIdData: {
            type: String | Number,
        }
    },
    data() {
        return {
            chart: null,
            isListData: [],
        }
    },
    watch: {
        groupIdData: {
            deep: true,
            handler(val) {
                // this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    created() {
        this.getAlertTotalByGroupIdFun();
    },
    methods: {
        getAlertTotalByGroupIdFun() {
            getAlertTotalByGroupId({
                groupId: this.groupIdData
            }).then((res) => {
                this.isListData = res.data
                this.initChart();
            })
        },
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.setOptions()
        },
        setOptions(numData) {
            // 绘制左侧面
            const wid = 18
            const w1 = Math.sin(Math.PI / 6) * wid //4
            const w2 = Math.sin(Math.PI / 3) * wid // 6.8
            const snapHeight = wid / 2
            const CubeLeft = echarts.graphic.extendShape({
                shape: {
                    x: 0,
                    y: 0
                },
                buildPath: function (ctx, shape) {
                    const xAxisPoint = shape.xAxisPoint
                    const c0 = [shape.x, shape.y]
                    const c1 = [shape.x - w2, shape.y]
                    const c2 = [shape.x - w2, xAxisPoint[1]]
                    const c3 = [shape.x, xAxisPoint[1]]
                    ctx
                        .moveTo(c0[0], c0[1])
                        .lineTo(c1[0], c1[1])
                        .lineTo(c2[0], c2[1])
                        .lineTo(c3[0], c3[1])
                        .closePath()
                }
            })
            // 绘制右侧面
            const CubeRight = echarts.graphic.extendShape({
                shape: {
                    x: 0,
                    y: 0
                },
                buildPath: function (ctx, shape) {
                    const xAxisPoint = shape.xAxisPoint
                    const c1 = [shape.x, shape.y]
                    const c2 = [shape.x, xAxisPoint[1]]
                    const c3 = [shape.x + w1, xAxisPoint[1] - w2 + snapHeight]
                    const c4 = [shape.x + w1, shape.y - w2 + snapHeight]
                    ctx
                        .moveTo(c1[0], c1[1])
                        .lineTo(c2[0], c2[1])
                        .lineTo(c3[0], c3[1])
                        .lineTo(c4[0], c4[1])
                        .closePath()
                }
            })
            // 绘制顶面
            const CubeTop = echarts.graphic.extendShape({
                shape: {
                    x: 0,
                    y: 0
                },
                buildPath: function (ctx, shape) {
                    //
                    const c1 = [shape.x, shape.y]
                    const c2 = [shape.x + w1, shape.y - w2 + snapHeight] //右点
                    const c3 = [shape.x - w2 + w1, shape.y - w2 + snapHeight]
                    const c4 = [shape.x - w2, shape.y]
                    ctx
                        .moveTo(c1[0], c1[1])
                        .lineTo(c2[0], c2[1])
                        .lineTo(c3[0], c3[1])
                        .lineTo(c4[0], c4[1])
                        .closePath()
                }
            })
            // 三个面图形
            echarts.graphic.registerShape('CubeLeft', CubeLeft)
            echarts.graphic.registerShape('CubeRight', CubeRight)
            echarts.graphic.registerShape('CubeTop', CubeTop)

            let yData = this.isListData.map(res => res.count)
            let MAX = [yData[0], yData[0], yData[0], yData[0], yData[0]]
            this.chart.setOption({
                // backgroundColor: '#000a3f',
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    backgroundColor: 'rgba(9, 24, 48, 0.5)',
                    borderColor: 'rgba(75, 253, 238, 0.4)',
                    textStyle: {
                        color: '#CFE3FC'
                    },
                    borderWidth: 1,
                    formatter: function (params) {
                        let item = ''
                        item += params[0].name

                        return item
                    }
                },
                grid: {
                    top: '10%',
                    left: '5%',
                    bottom: '5%',
                    right: '5%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: this.isListData.map(res => res.weekday),
                    axisTick: {
                        alignWithLabel: true
                    }
                }],
                yAxis: [{
                    type: 'value',
                    axisTick: {
                        show: false
                    }
                }],
                series: [
                    // 背景柱
                    {
                        type: 'custom',
                        renderItem: (params, api) => {
                            const location = api.coord([api.value(0), api.value(1)])
                            location[0] = location[0] + wid * 0
                            const xlocation = api.coord([api.value(0), 0])
                            xlocation[0] = xlocation[0] + wid * 0
                            return {
                                type: 'group',
                                children: [
                                    {
                                        type: 'CubeLeft',
                                        shape: {
                                            api,
                                            xValue: api.value(0),
                                            yValue: api.value(1),
                                            x: location[0],
                                            y: location[1],
                                            xAxisPoint: xlocation
                                        },
                                        style: {
                                            fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                                {
                                                    offset: 0,
                                                    color: 'rgba(0, 107, 188, 1)'
                                                },
                                                {
                                                    offset: 1,
                                                    color: 'rgba(0, 107, 188, 0.3)'
                                                }
                                            ])
                                        }
                                    },
                                    {
                                        type: 'CubeRight',
                                        shape: {
                                            api,
                                            xValue: api.value(0),
                                            yValue: api.value(1),
                                            x: location[0],
                                            y: location[1],
                                            xAxisPoint: xlocation
                                        },
                                        style: {
                                            fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                                {
                                                    offset: 0,
                                                    color: '#254193'
                                                },
                                                {
                                                    offset: 1,
                                                    color: '#254193'
                                                }
                                            ])
                                        }
                                    },
                                    {
                                        type: 'CubeTop',
                                        shape: {
                                            api,
                                            xValue: api.value(0),
                                            yValue: api.value(1),
                                            x: location[0],
                                            y: location[1],
                                            xAxisPoint: xlocation
                                        },
                                        style: {
                                            fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                                {
                                                    offset: 0,
                                                    color: 'rgba(129, 228, 255, 1)'
                                                },
                                                {
                                                    offset: 1,
                                                    color: 'rgba(0, 114, 221, 1)'
                                                }
                                            ])
                                        }
                                    }
                                ]
                            }
                        },
                        color: 'blue',
                        data: yData
                    }
                ]

            })
        }
    }
}
</script>
